<?php
/*
 * class mysql
*/
require_once ('db.adp.php');

class mysqlAdapter extends dbAdapter {
	
	function adp_init($cfg) {
		$this->_selectSchema = array_pop($cfg);
		$this->_conn = call_user_func_array("mysql_connect", $cfg);
		@mysql_set_charset('utf8', $this->_conn);
		@mysql_select_db($this->_selectSchema, $this->_conn);
	}
	/*
	 * function select
	 * @param $where,$selected
	*/
	
	function select($where = array() , $selected = "*", $ordered = NULL, $starts = NULL, $limits = NULL) {
		$orderby = "";
		if (!empty($where)) foreach ($where as $k => $v) {
			$ws[] = $k . "='" . $v . "'";
		}
		$ws = join(" and ", $ws);
		if (is_array($selected)) {
			$ss = join(" , ", $selected);
		}
		else {
			$ss = $selected;
		}
		if (isset($ordered)) {
			$orderby = sprintf(" order by %s", $ordered);
		}
		if (isset($starts)) {
			$limitsby = sprintf(" limit %s%s", $starts, isset($limits) ? ",{$limits}" : "");
		}
		else {
			$limitsby = "";
		}
		$__sql = sprintf("select {$ss} from " . $this->_table . " where {$ws}{$orderby}{$limitsby};");
		$__res = @mysql_query($__sql, $this->_conn);
		//var_dump(mysql_client_encoding($this->_conn));
		if (@mysql_num_rows($__res) > 0) {
			$rows = array();
			while ($row = mysql_fetch_array($__res)) {
				$rows[] = $row;
			}
			return $rows;
		}
		return NULL;
	}
	/*
	 * function insert
	 * @param $data
	 * @return false when fail or num affeced when succeed
	*/
	
	function insert($data) {
		parent::insert($data);
		$__keys = array_keys($data);
		$__vals = array_values($data);
		$__keys = join(',', $__keys);
		$__vals = F('warp', '\'', $__vals);
		$__vals = join(',', $__vals);
		$__sql = "INSERT INTO {$this->_table} ($__keys) VALUES ($__vals)";
		$this->_lastSQL;
		$__res = @mysql_query($__sql, $this->_conn);
		if ($__res == FALSE) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastAffectedRows = @mysql_affected_rows($this->_conn);
			return $this->_lastAffectedRows;
		}
	}
	/*
	 * function delete
	 * @param $where
	*/
	
	function delete($where) {
		foreach ($where as $k => $v) {
			$ws[] = $k . "='" . $v . "'";
		}
		$ws = join(" and ", $ws);
		$__sql = sprintf("delete from " . $this->_table . " where {$ws};");
		$__res = @mysql_query($__sql, $this->_conn);
		if ($__res == FALSE) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastAffectedRows = @mysql_affected_rows($this->_conn);
			return $this->_lastAffectedRows;
		}
	}
	/*
	 * function update
	 * @param $where,$datas
	*/
	
	function update($where, $datas) {
		foreach ($where as $k => $v) {
			$ws[] = $k . "='" . $v . "'";
		}
		$ws = join(" and ", $ws);
		foreach ($datas as $k => $v) {
			$ds[] = $k . "='" . $v . "'";
		}
		$ds = join(",", $ds);
		$__sql = "update " . $this->_table . " set {$ds} where {$ws};";
		$__res = @mysql_query($__sql, $this->_conn);
		if ($__res == FALSE) {
			$this->_lastSucceed = FALSE;
			return FALSE;
		}
		else {
			$this->_lastSucceed = TRUE;
			$this->_lastAffectedRows = @mysql_affected_rows($this->_conn);
			return $this->_lastAffectedRows;
		}
	}
	/**
	 *  update and insert
	 */
	
	function save() {
	}
	/**
	 *  plain sql query
	 */
	
	function query($sql) {
	}
	/**
	 *  存储过程
	 */
	
	function proc() {
	}
}
?>